import Vue from 'vue'
import Router from 'vue-router'
import Login from "../components/Login";
import Home from "../components/Home";
import Welcome from "../components/Welcome";
import User from "../components/User/User";
import Roles from "../components/Roles/Roles"
import Rights from "../components/Rights/Rights";
import Goods from "../components/Goods/goods";
import Params from "../components/Params/params";
import Categories from "../components/Categories/categories";
import Orders from "../components/Orders/orders";
import Reports from "../components/Reports/reports";

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          component: Welcome
        },
        {
          path: '/users',
          component: User
        },
        {
          path: '/roles',
          component: Roles
        },
        {
          path: '/rights',
          component: Rights
        },

        {
          path: '/goods',
          component: Goods
        },
        {
          path: '/params',
          component: Params
        },
        {
          path: '/categories',
          component: Categories
        },
        {
          path: '/orders',
          component: Orders
        },
        {
          path: '/reports',
          component: Reports
        },
      ]
    },
    {
      path: '/',
      redirect: '/login'
    }
  ]
})
//挂在守卫
/*
* to 将要访问路径
* from 从哪个路径跳转来的
* next 函数 放行
* */
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')//获取token
  if (!tokenStr) return next('/login')
  next()
})

export default router
